<?php

class odal_ResultIterator{
	var $m_aResultset;
	var $m_aCurrent;

	function &getResultset(){
		return $this->m_aResultset;
	}//getResultset

	function getResultCount(){
		reset( $this->m_aResultset );
		return count( $this->m_aResultset );
	}//getResultCount

	function &getCurrent(){
		return $this->m_aCurrent;
	}//getCurrent

	function setResultset( &$aResultset ){
		reset( $aResultset );
		$key = key( $aResultset ) ;
//		echo "[resultiteratort->setResultset] key: *" . $key . "*<br>";
//		echo "[resultiteratort->each] aResultset<br>\n: *";print_r( $aResultset );"<br>";
		$this->m_aResultset =& $aResultset;
		//$this->m_aCurrent = current ( $aResultset );
		$this->m_aCurrent =& $aResultset[$key];
	}//setResultset

	function setCurrent( $index ){
		reset( $this->m_aResultset );
//		while ( next( $this->m_aResultset ) && !( key( $this->m_aResultset ) == $index ) ){  
//			//next( $this->m_aResultset() );
//			$this->m_aCurrent =& $aResultset[key( prev( $this->m_aResultset ) )];
//		}//while
		while( list( $key, $dummy ) = each( $this->m_aResultset ) ){
			if ( $key == $index ){
				prev( $this->m_aResultset );
				$this->m_aCurrent =& $this->m_aResultset[key( $this->m_aResultset  )];
				break;
			}//if
		}//while
	}//setCurrent

	function &each( $debug = false ){
		$array_object =& $this->getCurrent();
//		echo "[resultiteratort->each] array_object<br>\n: *";print_r( $array_object );"<br>";
		next( $this->m_aResultset );
		$key = key( $this->m_aResultset );
//		echo "[resultiteratort->each] key: *" . $key . "*<br>";
		//$this->m_aCurrent =& next( $this->m_aResultset );
		$this->m_aCurrent =& $this->m_aResultset[$key];
		return $array_object; 
	}//each

/*********************************************************************************************** 	
	Verwendung von setSort:
	syntax:
	$new_array = $this->setSort( $array [, 'col1' [, SORT_FLAG [, SORT_FLAG]]]... );
	
	explanation:
	$array is the array you want to sort, 'col1' is the name of the column you want to sort, SORT_FLAGS are : SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
	you can repeat the 'col',FLAG,FLAG, as often you want, the highest prioritiy is given to the first - so the array is sorted by the last given column first, then the one before ...
	
	example:
	
	$array[0]['name'] = "Niko";
	$array[0]['age'] = 24;
	$array[0]['town'] = "Berlin";
	$array[1]['name'] = "Dennis";
	$array[1]['age'] = 34;
	$array[1]['town'] = "Berlin";
	$array[2]['name'] = "Oliver";
	$array[2]['age'] = 44;
	$array[2]['town'] = "Aachen";
	$array[3]['name'] = "Andi";
	$array[3]['age'] = 34;
	$array[3]['town'] = "Berlin";

	use the function like this:
	$array = $this->setSort( $array, 'town', 'age', SORT_DESC, 'name');
	
	here the resulting array would be: 
	$array[0]['name'] = "Oliver";
	$array[0]['age'] = 44;
	$array[0]['town'] = "Aachen";
	$array[1]['name'] = "Andi";
	$array[1]['age'] = 34;
	$array[1]['town'] = "Berlin";
	$array[2]['name'] = "Dennis";
	$array[2]['age'] = 34;
	$array[2]['town'] = "Berlin";
	$array[3]['name'] = "Niko";
	$array[3]['age'] = 24;
	$array[3]['town'] = "Berlin";
***********************************************************************************************/
	
	function setSort(){
		$args = func_get_args();
		$marray = array_shift($args);
		$msortline = "return(array_multisort(";
		foreach ($args as $arg) {
			$i++;
			if ( is_string( $arg ) ){
				foreach ( $marray as $row ) {
					$sortarr[$i][] = $row[$arg];
				}//foreach
			}else{
				$sortarr[$i] = $arg;
			}//else
			$msortline .= "\$sortarr[".$i."],";
		}//foreach
		$msortline .= "\$marray));";
		eval($msortline);
		return $marray;
	}//setSort

}//odal_ResultIterator

?>